package com.mxf.code.pojo.bo;

import com.alibaba.fastjson.annotation.JSONField;
import com.mxf.code.pojo.entity.SysUser;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : [mxf]
 * @version : [v1.0]
 * @className : LoginUserDetail
 * @description : [提供用户核心信息]
 * @createTime : [2022/9/3 18:07]
 * @updateUser : [mxf]
 * @updateTime : [2022/9/3 18:07]
 * @updateRemark : [描述说明本次修改内容]
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    private SysUser user;

    private List<String> permissions;
    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> simpleGrantedAuthorities;


    public LoginUser(SysUser user) {
        this.user = user;
    }

    public LoginUser(SysUser user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (simpleGrantedAuthorities != null && simpleGrantedAuthorities.size() > 0) {
            return simpleGrantedAuthorities;
        }
        simpleGrantedAuthorities = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return simpleGrantedAuthorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
